var createInstorage = new Instorage();

createInstorage.renderItems = function (combineObj) {
    var currentAction = this;
    var goodid = combineObj.goodid;
    //查询在月度计划明细里有没有该商品
    var alreadyExistsElements = $('#itemDetails').find('[goodid="' + goodid + '"]');
    if (alreadyExistsElements.length > 0) {
        alert('已经添加了该商品!!');
        return;
    }
    var template = $("#template").html();
    var rendered = Mustache.render(template, combineObj);
    $('#itemDetails').prepend(rendered);
    currentAction.calculate();
}

createInstorage.calculate = function () {
    var totalPrice = 0;
    $('#itemDetails tr').each(function () {
        var subtotal = 0;
        var row = $(this);
        var tdArr = row.find('td');
        var price = tdArr.eq(3).text();
        var goodnum = tdArr.eq(5).find('input[name="goodnum"]').eq(0).val();
        console.log('goodnum:', goodnum);
        console.log('price:', price);
        if (!/[1-9]\d*/.test(goodnum)) {
            tdArr.eq(5).find('input[name="goodnum"]').eq(0).val(1);
            subtotal += parseFloat(price);
        } else {
            subtotal += (parseFloat(price) * parseFloat(goodnum));
        }
        totalPrice += subtotal;
    });
    console.log('totalPrice:', totalPrice);
    $('#price_sum').html(parseFloat(totalPrice).toFixed(2));
}
createInstorage.loadOtherEvent = function(){
    $('#submitBtn').click(function () {
        var code = $('#code').val();
        var priceSum = $('#price_sum').text();
        var checkMan = $('#checkMan').val();
        var sendMan = $('#sendMan').val();
        var supplierId = $('#supplierId').val();
        var buyMan = $('#buyMan').val();
        if ($.trim(code) == '') {
            alert('入库单号不能为空!');
            return;
        }
        if ($.trim(supplierId) == '') {
            alert('供应商不能为空!');
            return;
        }
        if ($.trim(buyMan) == '') {
            alert('采购员不能为空!');
            return;
        }
        //组装数据
        var postData = {};
        postData['code'] = code;
        postData['checkMan'] = checkMan;
        postData['sendMan'] = sendMan;
        postData['supplierId'] = supplierId;
        postData['priceSum'] = priceSum;
        postData['buyMan'] = buyMan;
        //每一个节点
        var items = new Array();
        //遍历每行的数据
        $('#itemDetails tr').each(function () {
            var currentRow = $(this);
            var item = new Object();
            var inputArr = currentRow.find('input');
            inputArr.each(function (i, inputEle) {
                var name = $(inputEle).attr('name');
                var val = $(inputEle).val();
                item[name] = val;
            });
            items.push(item);
        });
        postData['items'] = items;
        $.ajax({
            type: "POST",
            dataType: 'json',
            url: "/instorage/add",
            data: {
                'instorageJson': JSON.stringify(postData) //stringify是在Ecma5也就是2011定义的
            },
            success: function (msg) {
                if (msg.code == '0') {
                    //返回列表页面
                    alert('保存成功');
                    window.location.href = '/instorage/list';
                } else if(msg.code == '1') {
                    alert('已经添加了该入库单，请检查状态!');
                }
                else {
                    alert('添加异常，请联系开发者!');
                }
            }
        });
    });

    //主页面的情况下需要 当前日期
    $('#currentDate').html(new Date().Format('yyyy年MM月dd日'));

    //按钮 返回列表
    $('#returnBtn').click(function () {
        window.location.href = '/instorage/list';
    });

};
createInstorage.init();

